<template>
  <div class="login">

    <!-- 登录页内容 -->
    <div class="login-content">
      <div class="login-img">
        <el-image :src="loginImage" style="width: 210px" />
      </div>
      <div class="login-form">
        <el-form ref="loginFormRef" :model="loginData" :rules="loginRules">
          <div class="form-title">
            <h2>护士日常工作系统</h2>
          </div>

          <!-- 用户名 -->
          <el-form-item prop="username">
            <div class="input-wrapper">
              <!-- <el-icon class="mx-2">
                <User />
              </el-icon> -->
              <svg-icon style="padding: 3px;" size="18px" iconClass="user"></svg-icon>
              <el-input ref="username" v-model="loginData.username" placeholder="登录名" name="username" size="large" />
            </div>
          </el-form-item>

          <!-- 密码 -->
          <el-tooltip :visible="isCapslock" :content="密码" placement="right">
            <el-form-item prop="password">
              <div class="input-wrapper">
                <!-- <el-icon class="mx-1">
                  <Lock />
                </el-icon> -->
                <svg-icon style="padding: 3px;" size="18px" iconClass="password"></svg-icon>
                <el-input v-model="loginData.password" placeholder="密码" :type="passType" name="password" size="large"
                  show-password   @keyup.enter="handleLoginSubmit" />
                <svg-icon @click="changeType" style="padding: 3px;cursor: pointer;" size="18px"
                  iconClass="eye"></svg-icon>
              </div>
            </el-form-item>
          </el-tooltip>

          <!-- 验证码 -->
          <el-form-item prop="captchaCode" style="height: 50px;">
            <!-- <div class="input-wrapper">
              <svg-icon icon-class="captcha" class="mx-2" />
              <el-input
                v-model="loginData.captchaCode"
                auto-complete="off"
                size="large"
                class="flex-1"
                :placeholder="验证码"
                @keyup.enter="handleLoginSubmit"
              />

              <el-image :src="captchaBase64" class="captcha-img" @click="getCaptcha" />
            </div> -->
          </el-form-item>

          <div style="height: 30px ;">

            <el-link type="primary" href="/forget-password">忘记密码</el-link>
          </div>

          <!-- 登录按钮 -->
          <el-button :loading="loading" type="primary" size="large" class="w-full" @click.prevent="handleLoginSubmit">
            登录
          </el-button>

        </el-form>
      </div>
    </div>

  </div>
</template>

<script setup>
// 外部库和依赖
import { useRoute } from "vue-router";
import router from "@/router";
import { reactive, toRefs, ref, getCurrentInstance, watch, onMounted, toRaw, watchEffect, computed } from "vue";
//Api
import { PLogin } from "../util/request"
import {useLoginStore} from '@/store/app.js'
 
const loginImage = ref(new URL("@/assets/images/login-image.svg", import.meta.url).href);

const route = useRoute();
// 按钮 loading 状态
const loading = ref(false);
// 是否大写锁定
const isCapslock = ref(false);

const loginData = ref({
  username: "",
  password: ""
});

const loginStore= useLoginStore();

//------密码框

const passType = ref('password')
const changeType = () => {
  console.log(passType.value)
  if (passType.value == 'password') {
    passType.value='text';
  }else{
    passType.value='password';
  }
}


 
const loginFormRef = ref();

const loginRules = computed(() => {
  return {
    username: [
      {
        required: true,
        trigger: "blur",
        message: "请输入登录名",
      },
    ],
    password: [
      {
        required: true,
        trigger: "blur",
        message: "请输入密码",
      }
    ]
  };
});
//获取用户信息

//提交登录
function handleLoginSubmit() {
  console.log('点击登录');
  loginFormRef.value?.validate((valid) => {
    if (valid) {
      loading.value = true;
      loginStore.Login(loginData.value)
        .then((res) => {
          console.log(res)
          console.log('登录')
          // UserStore.setData(res)
          // localStorage.setItem('CurUser', res.username);
         // const { path, queryParams } = parseRedirect();
          //console.log('页面跳转' + path + ":" + queryParams);
         // router.push({ path: path, query: queryParams });
          router.push({ path: '/' });
        })
        .catch(() => {
          console.log('出现错误');
        })
        .finally(() => {
          loading.value = false;
        });
    }
  });
}

/** 解析 redirect 字符串 为 path 和  queryParams */
function parseRedirect() {
  const query = route.query;
  console.log(query)
  const redirect = query.redirect ?? "/";

  const url = new URL(redirect, window.location.origin);
  const path = url.pathname;
  const queryParams = {};

  url.searchParams.forEach((value, key) => {
    queryParams[key] = value;
  });

  return { path, queryParams };
}

onMounted(() => {

  parseRedirect()

})

</script>


<style lang="scss" scoped>
.login {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  overflow-y: auto;
  background: url("@/assets/images/login-background-light.jpg") no-repeat center right;

  .login-header {
    position: absolute;
    top: 0;
    display: flex;
    justify-content: right;
    width: 100%;
    padding: 15px;

    .logo {
      width: 26px;
      height: 26px;
    }

    .title {
      margin: auto 5px;
      font-size: 24px;
      font-weight: bold;
      color: #3b82f6;
    }
  }

  .login-content {
    display: flex;
    width: 960px;
    overflow: hidden;
    background-color: #fff;
    border-radius: 5px;
    box-shadow: var(--el-box-shadow-light);

    @media (width <=768px) {
      flex-direction: column;
      max-width: 100%;
      height: 100vh;
      padding: 0 30px;
      border-radius: 0;
      box-shadow: none;
    }

    .login-img {
      display: flex;
      flex: 3;
      align-items: center;
      justify-content: center;
      background: linear-gradient(60deg, #165dff, #6aa1ff);

      @media (width <=768px) {
        display: none;
      }
    }

    .login-form {
      display: flex;
      flex: 2;
      flex-direction: column;
      justify-content: center;
      min-width: 400px;
      padding: 30px;

      @media (width <=768px) {
        width: 100%;
        padding: 0 20px;
      }

      .form-title {
        position: relative;
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 0 0 20px;
        text-align: center;
      }

      .input-wrapper {
        display: flex;
        align-items: center;
        width: 100%;
      }

      .captcha-img {
        height: 48px;
        cursor: pointer;
        border-top-right-radius: 6px;
        border-bottom-right-radius: 6px;
      }

      .third-party-login {
        display: flex;
        justify-content: center;
        width: 100%;
        color: var(--el-text-color-secondary);

        *:not(:first-child) {
          margin-left: 20px;
        }

        .icon {
          cursor: pointer;
        }
      }
    }
  }

  .login-footer {
    position: absolute;
    bottom: 0;
    width: 100%;
    text-align: center;
  }
}

:deep(.el-form-item) {
  background: var(--el-input-bg-color);
  border: 1px solid var(--el-border-color);
  border-radius: 5px;
}

:deep(.el-input) {
  .el-input__wrapper {
    padding: 0;
    background-color: transparent;
    box-shadow: none;

    &.is-focus,
    &:hover {
      box-shadow: none !important;
    }

    input:-webkit-autofill {
      /* 通过延时渲染背景色变相去除背景颜色 */
      transition: background-color 1000s ease-in-out 0s;
    }
  }
}
</style>